﻿using SIR.Common;
using SIR.Common.Condition;
using SIR.DAL;
using SIR.Entities.Interfaces;
using System.Linq;

namespace SIR.Entities.Implementations
{
	/// <summary>
	/// Identifies a favorite image for a user
	/// </summary>
	public class FavoriteImage : Entity, IFavoriteImage
	{
		private IImage _image;
		private IUser _user;

		/// <summary>
		/// Gets the image reference for the favorite image
		/// </summary>
		[Newtonsoft.Json.JsonIgnore]
		[DatabaseAccess(DatabaseAccessRights.None)]
		public IImage Image
		{
			get
			{
				if (_image == null)
				{
					_image = DataServer.Find<Image>(new Predicate("ImageId", ImageId, PredicateOperator.EqualTo)).Results.FirstOrDefault();
				}
				return _image;
			}
		}
		/// <summary>
		/// Gets the user reference for the favorite image
		/// </summary>
		[Newtonsoft.Json.JsonIgnore]
		[DatabaseAccess(DatabaseAccessRights.None)]
		public IUser User
		{
			get
			{
				if (_user == null)
				{
					_user = DataServer.Find<User>(new Predicate("UserId", UserId, PredicateOperator.EqualTo)).Results.FirstOrDefault();
				}
				return _user;
			}
		}
		/// <summary>
		/// Gets the referenced image Id
		/// </summary>
		[DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public long ImageId { get; set; }
		/// <summary>
		/// Gets the referenced user Id
		/// </summary>
		[DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public long UserId { get; set; }
	}
}
